package cn.wjdiankong.levideo.list.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.data.ClearCacheData;

public class ClearCacheListAdapter extends BaseAdapter{
	
	private List<ClearCacheData> dataList = new ArrayList<ClearCacheData>();
	private Context mContext;
	
	public ClearCacheListAdapter(Context ctx, List<ClearCacheData> dataList){
		mContext = ctx;
		this.dataList = dataList;
	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@SuppressLint({ "ViewHolder", "InflateParams" })
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		View view = convertView;
		if (view == null || view.getTag() == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.clear_cache_list_item, null);
			viewHolder = new ViewHolder();
			viewHolder.logoImg = (ImageView) view.findViewById(R.id.img);
			viewHolder.sizeTxt = (TextView) view.findViewById(R.id.cache_txt);
			viewHolder.nameTxt = (TextView) view.findViewById(R.id.name_txt);
			viewHolder.clearBtn = (Button) view.findViewById(R.id.clear_btn);
		}else{
			viewHolder = (ViewHolder) view.getTag();
		}
		if(dataList.get(position).logo <= 0){
			viewHolder.logoImg.setVisibility(View.GONE);
		}else{
			viewHolder.logoImg.setVisibility(View.VISIBLE);
			viewHolder.logoImg.setImageResource(dataList.get(position).logo);
		}
		viewHolder.sizeTxt.setText(dataList.get(position).cacheSize);
		viewHolder.nameTxt.setText(dataList.get(position).name);
		viewHolder.clearBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.click(position);
				}
			}
		});
		return view;
	}
	
	private ClickItemBtnListener mListener;
	public void setClickListener(ClickItemBtnListener listener){
		mListener = listener;
	}
	
	public interface ClickItemBtnListener{
		void click(int position);
	}
	
	public static class ViewHolder{
		public TextView nameTxt;
		public ImageView logoImg;
		public TextView sizeTxt;
		public Button clearBtn;
	}

}
